#!/bin/bash
#安装mysql8
#作者: x
#时间: 2023-12-11
#原文链接 https://cloud.tencent.com/developer/article/1745084
#变量
#密码
MYSQL_ROOT_PASSWORD=Dev@sd123
echo "=======start install mysql8========"
yum -y update
for i in $(rpm -qa|grep mysql);do rpm -e $i --nodeps;done
rm -rf /var/lib/mysql && rm -rf /etc/my.cnf
yum -y localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
yum install -y mysql-community-server --nogpgcheck
systemctl start mysqld
systemctl enable mysqld
#修改密码 注意： 如果不是第一次安装则有可能获取到多个密码，请根据日志中显示的密码进行修改
tmp_passwd=`cat /var/log/mysqld.log |grep "A temporary password is generated for root"|awk -F ' ' '{print $NF}'|sed 's/[[:space:]]//g'`
echo "mysql current password  is "  "$tmp_passwd"
mysql -u root -p"$tmp_passwd" --connect-expired-password -e "alter user 'root'@'localhost' identified by "${MYSQL_ROOT_PASSWORD}"; use mysql; update user set host='%'; FLUSH PRIVILEGES;"
echo "new password is " "$MYSQL_ROOT_PASSWORD"
echo "=====install end==========="
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
